package com.app.asktom.component;

import java.util.ArrayList;
import java.util.List;

import com.app.asktom.model.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



//AskTomDBHelper askTomDBHelper = new AskTomDBHelper(this);
//askTomDBHelper.addUser(new User("7777"));
//askTomDBHelper.addUser(new User("5555"));
//
//List<User> userList = askTomDBHelper.getAllUser();
//
//for(int i = 0; i < userList.size(); i++) {
//	User user1 = userList.get(i);
//	Log.d("user"+i,user1.toString());
//}

//User user = askTomDBHelper.getUser();

public class AskTomDBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "asktom.db";
	private static final int DATABASE_VERSION = 1;	
	
	private static final String TABLE_USER = "tbl_user";
	private static final String COLUMN_ID = "userid";
	private static final String COLUMN_PHONE = "phone";
	private static final String COLUMN_PASSWORD = "password";
	
	
	private static final String TABLE_PHONE = "tbl_phone";
	
	private static final String DATABASE_CREATE = "Create table IF NOT EXISTS " + TABLE_USER  + 
			" (" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_PHONE + " text not null, " +
			COLUMN_PASSWORD + " text not null);";
	
	private static final String PHONE_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_PHONE +
			" (phoneNumber integer primary key,  text not null);";
	
	public AskTomDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
		db.execSQL(PHONE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		
	}

    // Adding new user
	public void addUser(User user) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("phone", user.getPhone());
		db.insert(TABLE_USER, null, values);
		db.close();
	}
	
	public void deleteUser() {
		String sql = "DELETE FROM " + TABLE_USER + " WHERE " + COLUMN_ID + " = 1";
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);
	}
	
	/**
	public User getUser() {
		User user = new User();
		String sql = "SELECT " + COLUMN_ID + ", " + COLUMN_PHONE + 
				" FROM " + TABLE_USER + " WHERE " + COLUMN_ID + " = 1";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()) {
			user.setUserid(Integer.parseInt(cursor.getString(0)));
			user.setPhone(cursor.getString(1));
		}
		cursor.close();
		db.close();
		return user;
	}*/

	/**
	public List<User> getAllUser() {
		List<User> userList = new ArrayList<User>();
		String sql = "SELECT userid, phone FROM " + TABLE_USER;
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			do {
				User user = new User();
				user.setUserid(Integer.parseInt(cursor.getString(0)));
				user.setPhone(cursor.getString(1));
				userList.add(user);
			} while (cursor.moveToNext());			
		}
		cursor.close();
		db.close();
		return userList;
	}*/
	
	public void dropTable() {
		String sql = "DROP TABLE " + TABLE_USER;
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL(sql);
		db.close();
	}
	
	
}
